import { axios } from "../../utils/request"
import Dialog from "@vant/weapp/dialog/dialog"
import Toast from "@vant/weapp/toast/toast"
Page({
  /**
   * 页面的初始数据
   */
  data: {
    active: 0,
    initData: [] as any,
    order: {},
    show: false,
    list: [1, 2, 3, 4, 5, 6, 7, 8, 9],
    password: <Array<number>>[],
  },
  async init() {
    const isLogin = wx.getStorageSync("isLogin")
    if (isLogin) {
      wx.showLoading({ title: "加载中", mask: true })
      const id = wx.getStorageSync("userInfo").id
      let data = (await axios(`/order/${id}`)) as Response
      this.setData({
        initData: data.data,
      })
      setTimeout(function () {
        wx.hideLoading()
      }, 200)
    }
  },
  async finish(e: any) {
    let index = e.currentTarget.dataset.index
    Dialog.confirm({
      title: "是否完成",
      message: "点击确认将完成此订单",
    })
      .then(async () => {
        await axios(`/order/${index}`, "PATCH", {
          status: 3,
        })
        this.init()
      })
      .catch(() => {
        wx.showToast({
          title: "已取消",
          icon: "none",
        })
      })
  },
  comment(e: any) {
    let index = e.currentTarget.dataset.index
    let current = this.data.initData[index]
    wx.navigateTo({
      url: "/pages/comment/comment",
      success: (res) => {
        // 通过 eventChannel 向被打开页面传送数据
        res.eventChannel.emit("context", current)
      },
    })
  },
  async onChange(e: any) {
    wx.showLoading({ title: "加载中", mask: true })
    let index = e.detail.index
    let isLogin = wx.getStorageSync("isLogin")
    if (isLogin) {
      const id = wx.getStorageSync("userInfo").id
      if (index == 0) {
        let data = (await axios(`/order/${id}`)) as Response
        this.setData({
          initData: data.data,
        })
      }
      if (index == 1) {
        let data = (await axios(`/order/useful/${id}`)) as Response
        this.setData({
          initData: data.data,
        })
      }
      if (index == 2) {
        let data = (await axios(`/order/unpay/${id}`)) as Response
        this.setData({
          initData: data.data,
        })
      }
    }
    setTimeout(function () {
      wx.hideLoading()
    }, 200)
  },
  repay(e: any) {
    let index = e.currentTarget.dataset.index
    let order = this.data.initData[index]
    this.setData({
      order,
      show: true,
    })
  },
  showPopup() {
    Toast.loading({
      message: "支付中...",
      duration: 1000,
      forbidClick: true,
      loadingType: "spinner",
      onClose: () => {
        this.setData({
          show: true,
        })
      },
    })
  },
  async closePopup() {
    this.setData({ show: false })
    let order = this.data.order as any
    order.status = 5
    await axios("/order/unpay", "POST", {
      ...this.data.order,
    })
    wx.reLaunch({ url: "/pages/order/order" })
  },
  async getPassword(e: any) {
    if (this.data.password.length == 5) {
      this.data.password.push(e.currentTarget.dataset.number)
      this.setData({ password: this.data.password, show: false })
      let order = this.data.order as any
      order.status = 1
      try {
        await axios("/order", "POST", {
          ...this.data.order,
        })
        Toast.success({
          type: "success",
          message: "提交成功",
          duration: 1000,
          onClose: async () => {
            this.setData({ password: [] })
            wx.reLaunch({ url: "/pages/order/order" })
          },
        })
      } catch (error) {
        Toast.success({
          type: "fail",
          message: "提交失败",
          duration: 1000,
          onClose: async () => {
            this.setData({ password: [] })
          },
        })
      }
    } else if (this.data.password.length < 5) {
      this.data.password.push(e.currentTarget.dataset.number)
      this.setData({ password: this.data.password })
    } else {
      return
    }
  },
  removePassword() {
    this.data.password.pop()
    this.setData({ password: this.data.password })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  async onShow() {
    const isLogin = wx.getStorageSync("isLogin")
    if (isLogin) {
      this.init()
    } else {
      wx.navigateTo({ url: "/pages/login/login" })
    }
  },
})
